#ifndef PREVIEW_WIDGET_H
#define PREVIEW_WIDGET_H
#include "ui_PreviewWidget.h"
#include <DataStructures.h>
// QIODevice, QSize, QPixmap, etc:
#include <QPicture>


#ifndef NOPREVIEW
#include <QThread>
class PreviewCreator : public QThread {
	Q_OBJECT
public:
	PreviewCreator(QIODevice *_image, const QSize &previewSize)
		: QThread(), image(_image), size(previewSize) { }
	void run();
private:
	QIODevice *image;
	QSize size;
signals:
	void previewDone(QImage preview);
};
#endif //NOPREVIEW


class PreviewWidget : public QWidget
{
	Q_OBJECT
	Q_PROPERTY(QString imageName READ imageName/* WRITE setImageName*/)
	Q_PROPERTY(qint64 size READ size/* WRITE setSize*/)
	Q_PROPERTY(qint64 listPosition READ listPosition WRITE setListPosition)
	Q_PROPERTY(QString mimeType READ mimeType/* WRITE setMimeType*/)
	Q_PROPERTY(bool canceled READ isCanceled/* WRITE setCanceled*/)
	Q_PROPERTY(bool done READ done/* WRITE setDone*/)
	Q_PROPERTY(bool toDelete READ toDelete WRITE setToDelete)
	Q_PROPERTY(qint64 lastDelta READ lastDelta)
public:
	PreviewWidget(QWidget *parent, QIODevice *image, QString ImageName, QString mime);
	inline qint64 size() const { return this->image_.size; }
	inline QString imageName() const { return this->image_.ImageName; }
	inline QString mimeType() const { return this->image_.mimeType; }
	inline bool isCanceled() const { return this->canceled_; }
	inline bool done() const { return this->done_; }
	inline bool toDelete() const { return this->todelete_; }
	inline void setToDelete(bool d) { this->todelete_ = d; }
	inline qint64 listPosition() const { return this->image_.index; }
	inline void setListPosition(qint64 pos) { this->image_.index = pos; }
	inline ImageToUpload image() const { return this->image_; }
	inline void enableRemoveButton() { this->ui.RemoveButton->setEnabled(true); }
	inline void disableRemoveButton() { this->ui.RemoveButton->setEnabled(false); }
#ifndef NOPREVIEW
	inline QSize previewSize() const { return this->ui.PreviewLabel->size(); }
	inline const QPixmap* previewPxm() const { return this->ui.PreviewLabel->pixmap(); }
#endif
	void setProgress(int val, int max);
	qreal currentProgress() const;
	inline qint64 lastDelta() const { return lastDelta_; }
private:
	inline QIODevice* Image() { return this->image_.Image; }
	ImageToUpload image_;
	Ui::PreviewWidget ui;
#ifndef NOPREVIEW
	PreviewCreator *pc;
#endif
	bool canceled_;
	bool done_;
	bool todelete_;
	qint64 lastDelta_;
#ifndef NOIMAGEWINDOWPREVIEW
protected:
	void mouseDoubleClickEvent(QMouseEvent *event); // open image in viewer
#endif
signals:
	void removeMe(qint64);
	void removeMe(PreviewWidget*);
	void clicked();
public slots:
	void uploadStarted();
	void uploadCanceled();
	void uploadFinished();
	void uploadError(QString error);
#ifndef NOPREVIEW
	inline void setPreview(QPixmap *pixm) { ui.PreviewLabel->setPixmap(
							pixm->scaled(
								ui.PreviewLabel->size(),
								Qt::KeepAspectRatio,
								Qt::SmoothTransformation
								)
							);
							pc->wait();
	}
	inline void setPreview(QImage image) { ui.PreviewLabel->setPixmap(
							QPixmap::fromImage(image)
							);
							pc->wait();
	}
#endif
private slots:
	void removeMe_();

private:
};


#endif //PREVIEW_WIDGET_H
